package com.anyou.mall.dri.controller;

import com.anyou.common.annotation.Log;
import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.core.page.TableDataInfo;
import com.anyou.common.enums.BusinessType;
import com.anyou.common.exception.ServiceException;
import com.anyou.common.utils.poi.ExcelUtil;
import com.anyou.mall.dri.domain.Driver;
import com.anyou.mall.dri.domain.Team;
import com.anyou.mall.dri.param.EnableParam;
import com.anyou.mall.dri.service.IDriverService;
import com.anyou.mall.dri.service.ITeamService;
import com.anyou.mall.dri.vo.DriverDetailsVo;
import com.anyou.mall.dri.vo.DriverOrderNumVo;
import com.anyou.mall.dri.vo.DriverOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 司机列表Controller
 *
 * @author gaochaohao
 * @date 2022-04-02
 */
@Api(tags = "司机列表")
@RestController
@RequestMapping("/dri/driver")
public class DriverController extends BaseController
{
    @Autowired
    private IDriverService driverService;
    @Autowired
    private ITeamService teamService;

    /**
     * 查询司机列表列表
     */
    @ApiOperation("查询司机列表列表")
    @PreAuthorize("@ss.hasPermi('dri:driver:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Driver>> list(Driver driver)
    {
        startPage();
        List<Driver> list = driverService.selectDriverList(driver);
        return getDataTable(list);
    }
    /**
     * 查询配送团队列表
     */
    @ApiOperation("查询配送团队列表")
    @PreAuthorize("@ss.hasPermi('dri:driver:teamlist')")
    @GetMapping("/teamlist")
    public ApiResult<List<Team>> list(Team team)
    {
        List<Team> teams = teamService.selectTeamList(new Team());
        return ApiResult.success(teams);
    }
    /**
     * 导出司机列表列表
     */
    @ApiOperation("导出司机列表列表")
    @PreAuthorize("@ss.hasPermi('dri:driver:export')")
    @Log(title = "司机列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Driver driver)
    {
        List<Driver> list = driverService.selectDriverList(driver);
        ExcelUtil<Driver> util = new ExcelUtil<Driver>(Driver.class);
        util.exportExcel(response, list, "司机列表数据");
    }

    /**
     * 获取司机列表详细信息
     */
    @ApiOperation("获取司机列表详细信息")
    @PreAuthorize("@ss.hasPermi('dri:driver:query')")
    @GetMapping(value = "/{driverId}")
    public ApiResult<DriverDetailsVo> getInfo(@PathVariable("driverId") Long driverId)
    {
        return ApiResult.success(driverService.getDriverDetailsByDriverId(driverId));
    }
    /**
     * 修改司机启用状态
     */
    @ApiOperation("修改司机启用状态")
    @PreAuthorize("@ss.hasPermi('dri:driver:editEnable')")
    @GetMapping(value = "/editEnable")
    public ApiResult editEnable(@Valid EnableParam param)
    {
        driverService.editEnable(param);
        return ApiResult.success();
    }

    /**
     * 新增司机列表
     */
    @ApiOperation("新增司机列表")
    @PreAuthorize("@ss.hasPermi('dri:driver:add')")
    @Log(title = "司机列表", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult add(@RequestBody Driver driver)
    {
        return ApiResult.success(driverService.insertDriver(driver));
    }

    /**
     * 修改司机列表
     */
    @ApiOperation("修改司机列表")
    @PreAuthorize("@ss.hasPermi('dri:driver:edit')")
    @Log(title = "司机列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult edit(@RequestBody Driver driver)
    {
        return ApiResult.success(driverService.updateDriver(driver));
    }

    /**
     * 删除司机列表
     */
    @ApiOperation("删除司机列表")
    @PreAuthorize("@ss.hasPermi('dri:driver:remove')")
    @Log(title = "司机列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{driverIds}")
    public ApiResult remove(@PathVariable Long[] driverIds)
    {
        return ApiResult.success(driverService.deleteDriverByDriverIds(driverIds));
    }

    /**
     * 修改司机状态
     */
    @ApiOperation("修改司机状态")
    @PreAuthorize("@ss.hasPermi('dri:driver:enableStatus')")
    @Log(title = "司机列表",businessType = BusinessType.UPDATE)
    @PostMapping("/{driverId}")
    public ApiResult enableStatus(@PathVariable("driverId") Long driverId) {
        return driverService.enableStatus(driverId);
    }

    /**
     * 司机详情-配送订单列表
     */
    @ApiOperation("司机详情-配送订单列表")
    @PreAuthorize("@ss.hasPermi('dri:driver:driverOrderPage')")
    @PostMapping("/driverOrderPage")
    public TableDataInfo<List<DriverOrderVo>> driverOrderPage( DriverOrderVo driverOrderVo) {
        Driver driver = driverService.selectDriverByDriverId(driverOrderVo.getDriverId());
        if(driver==null){
            throw new ServiceException("司机账号不存在");
        }
        Integer driverType = Optional.ofNullable(driver.getDriverType()).orElse(-1);//0-揽件员 1-配送员
        driverOrderVo.setDriverType(driverType);
        startPage();
        List<DriverOrderVo> list = driverService.deliveryOrderPage(driverOrderVo);
        return getDataTable(list);
    }

    /**
     * 司机详情-已完成揽件/配送订单数量
     */
    @ApiOperation("司机详情-已完成揽件或配送订单数量")
    @PreAuthorize("@ss.hasPermi('dri:driver:driverOrderNum')")
    @PostMapping("/driverOrderNum")
    public ApiResult driverOrderNum(@Valid @RequestBody DriverOrderNumVo driverOrderNumVo) {
        return driverService.driverOrderNum(driverOrderNumVo);
    }
}
